package com.btcode.common;

import com.btcode.log.DefaultLogFactory;
import com.btcode.log.ILog;
import com.btcode.log.ILogFactory;

public final class MyLog {

    private volatile static MyLog instance;

    private ILogFactory logFactory;

    private MyLog() {
        logFactory = new DefaultLogFactory();
    }

    public static MyLog getInstance() {
        if (instance == null) {
            synchronized (MyLog.class) {
                if (instance == null) {
                    instance = new MyLog();
                }
            }
        }
        return instance;
    }

    public void setLogFactory(ILogFactory logFactory) {
        this.logFactory = logFactory;
    }

    private String logRootPath = "";

    public void setRootPath(String rootPath) {
        logRootPath = rootPath;
    }

    public ILog getLogger(String name) {
        ILog log = logFactory.getLog(name, loadLogRootPath());
        return log;
    }


    public ILog getLogger(Class<?> myClass) {
        return getLogger(myClass.getName());
    }

    public String loadLogRootPath() {
        if (logRootPath == null || logRootPath.trim().equals("")) {
            throw new RuntimeException("请设置MyLog的logRootPath");
        }
        return logRootPath;
    }

}
